let Cesium:any=undefined;

/**
 * 多线段尾链
 * @global
 * @param {object} options
 * @param {number} options.duration - 持续时长
 * @param {number} options.speed - 速度
 * @param {Color} options.color - 颜色
 * @param {string} options.image - 图片
 * @see {@link module:Graphics#craeteCorridorGraphics|craeteCorridorGraphics}
 * @example
 * import { Graphics } from 'cesium_dev_kit'
 * const graphicObj = new Graphics({
 *     cesiumGlobal: Cesium,
 *     containerId: 'cesiumContainer'
 * })
 * // 创建材质
 * const  polylineMaterial = new Cesium.Scene.PolylineTrailLinkMaterial({
          color: Cesium.Color.BLUE.withAlpha(0.4),
          duration: 15000,
          image: 'static/data/images/Textures/river.png',
          speed: 0.5
        })
 *
 * graphicObj.graphics.craeteCorridorGraphics({
 *     positions: Cesium.Cartesian3.fromDegreesArray(
 *            110.16018735617934, 31.036076859828338,
 *            110.17845812703679,31.033686527335444,
 *            110.19261040403379, 31.02892781958261,
 *            110.20775152895165,31.02475678594998,
 *            110.2247484728178,31.019315371809977,
 *            110.24033095328579,31.017866033294506),
        width: 1500.0,
        height: 0,
        extrudedHeight: 50，
        cornerType: Cesium.CornerType.MITERED,
        outline: false,
        material:polylineMaterial
 * })
 */
export class PolylineTrailLinkMaterial extends Cesium.Property {
    private _definitionChanged:any;
    private _color:any;
    private _colorSubscription: any;
    private _time: any;
    private _duration: any;
    public image: any;
    public speed: any;
    public isTranslucent: any;

    constructor(options: any) {
        super()
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
        this._definitionChanged = new Cesium.Event();
        this._color = options.color || Cesium.Color.WHITE;
        // waterImg= this.getDfSt(["primitive", "WaterPrimitive"]);   this.image = options.image || waterImg;
        this.image = options.image;
        this._duration = options.duration || 3000;
        this._time = new Date().getTime();
        this.speed = options.speed; // speed, larger is faster
        this.isTranslucent = function () {
            return true;
        };

    }
// @ts-ignore
    get isConstant() {
        return false;
    }
// @ts-ignore
    get definitionChanged() {
        return this._definitionChanged;
    }

    set color(value: any) {
        this._color = value;
    }

    get color() {
        return this._color;
    }

    set duration(value: any) {
        this._duration = value;
    }

    get duration() {
        return this._duration;
    }

    getType() { // 'PolylineTrailLinkMaterial'
        // @ts-ignore
        return Cesium.Material.PolylineTrailLinkType;
    }

    getValue(time: any, result: any) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, this.color, result.color);
        // @ts-ignore
        result.image = Cesium.Material.PolylineTrailLinkImage;
        result.time = (((new Date().getTime() - this._time) % this.duration) / this.duration) * this.speed;
        return result;
    }

    equals(other: any) {
        return this === other || (other instanceof PolylineTrailLinkMaterial && Cesium.Property.equals(this._color, other._color));
    }

}

function getDfSt(dfSt: any, attrArr: any) {
    if (dfSt) {
        const res = attrArr.reduce((pre: any, curr: any) => {
            if (pre) {
                return pre[curr];
            } else {
                return dfSt[curr];
            }
        }, undefined);
        return dfSt.baseService + res;
    }
    return undefined;
}

const _installPolylineTrailLinkMaterial = (dfSt: any,CesiumSources:any) => {
  Cesium=CesiumSources;

    let waterImg = getDfSt(dfSt, ["primitive", "WaterPrimitive"]);
    // @ts-ignore
    Cesium.Material.PolylineTrailLinkType = "PolylineTrailLinkMaterial";
    // @ts-ignore
    Cesium.Material.PolylineTrailLinkSource = `
    uniform vec4 color;
    uniform float time;
    uniform sampler2D image;
    czm_material czm_getMaterial(czm_materialInput materialInput)
    {
        czm_material material = czm_getDefaultMaterial(materialInput);
        vec2 st = materialInput.st;
        vec4 colorImage = texture(image, vec2(fract(st.s - time), st.t));
        material.alpha = colorImage.a * color.a;
        material.diffuse = (colorImage.rgb+color.rgb)/2.0;
        return material;
    }`;
    // @ts-ignore
    Cesium.Scene.PolylineTrailLinkMaterial = PolylineTrailLinkMaterial;
    // @ts-ignore
    Cesium.Material.PolylineTrailLinkImage = waterImg || "static/data/images/Textures/river.png";
    // @ts-ignore
    Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineTrailLinkType, {
        fabric: {
            // @ts-ignore
            type: Cesium.Material.PolylineTrailLinkType,
            uniforms: {
                color: new Cesium.Color(1.0, 1.0, 1.0, 1),
                // @ts-ignore
                image: Cesium.Material.PolylineTrailLinkImage,
                time: -20,
            },
            // @ts-ignore
            source: Cesium.Material.PolylineTrailLinkSource,
        },
        translucent: function () {
            return true;
        },
    });
}

export {
    _installPolylineTrailLinkMaterial
};

